package com.agriculture.controller;

import com.agriculture.common.OrderStatus;
import com.agriculture.common.R;
import com.agriculture.entity.Orders;
import com.agriculture.entity.OrderGood;
import com.agriculture.service.OrderGoodService;
import com.agriculture.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/ordergood")
@CrossOrigin
@Slf4j
public class OrderGoodController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderGoodService orderGoodService;

    /**
     * 根据 user_id 和 order_id 新增数据
     * @param orderGood
     * @return
     */
    @PostMapping("/add")
    public R addOrderGood(@RequestBody OrderGood orderGood){
        orderGoodService.save(orderGood);
        return R.success("插入成功！");
    }

    /**
     * 对 order_good 进行分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long userId){

        LambdaQueryWrapper<Orders> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(Orders::getUserId, userId);
        orderWrapper.eq(Orders::getStatus, OrderStatus.ToBePaid);
        Orders order = orderService.getOne(orderWrapper);

        //分页构造器
        Page<OrderGood> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<OrderGood> queryWrapper = new LambdaQueryWrapper<>();

        //添加排序条件，根据CreateTime进行排序
        queryWrapper.orderByAsc(OrderGood::getCreateTime);
        queryWrapper.eq(OrderGood::getOrderId, order.getId());
        //分页查询
        orderGoodService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改 order_good
     * @param orderGood
     * @return
     */
    @PutMapping
    public R updateOrderGood(@RequestBody OrderGood orderGood){
        orderGoodService.updateById(orderGood);
        return R.success("修改成功！");
    }

    /**
     * 根据 id 硬删除 order_good
     * @param id
     * @return
     */
    @DeleteMapping
    public R delById(@PathVariable Long id){
        orderGoodService.removeById(id);
        return R.success("删除成功！");
    }

    /**
     * 根据 order_id 查询 order_good
     * @param orderId
     * @return
     */
    @GetMapping("/getByOrderId")
    public R getByOrderId(Long orderId){
        LambdaQueryWrapper<OrderGood> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderGood::getOrderId, orderId);
        List<OrderGood> list = orderGoodService.list(queryWrapper);
        return R.success(list);
    }

}
























